﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Heckel.EasyTools.Diagramming.Utilities
{
    public enum DateInterval { Year, Month, Weekday, Day, Hour, Minute, Second }

    public static class SearchUtils
    {
        public static Control RecursivelyFindControl(Control root, string id)
        {
            if (root.ID == id)
                return root;

            foreach (Control c in root.Controls)
            {
                Control t = RecursivelyFindControl(c, id);
                if (t != null)
                    return t;
            }

            return null;
        }

        public static void RecursivelyModifyWebControls(ref Control root, Type controlToFind, string eventToHandle, string eventText, string filterID)
        {
            if (root.GetType().Equals(controlToFind))
            {
                if (root.ID.IndexOf(filterID) > -1)
                    ((WebControl)root).Attributes.Add(eventToHandle, eventText);
            }

            for (int cx = 0; cx < root.Controls.Count; cx++)
            {
                Control c = root.Controls[cx];
                RecursivelyModifyWebControls(ref c, controlToFind, eventToHandle, eventText, filterID);
            }
        }

        public static void RecursivelyFindWebControls(Control root, Type controlToFind, string filterID, ref List<WebControl> finder)
        {
            if (root.GetType().Equals(controlToFind))
            {
                if (root.ID.IndexOf(filterID) > -1)
                    finder.Add((WebControl)root);
                else
                    finder.Add((WebControl)root);
            }

            for (int cx = 0; cx < root.Controls.Count; cx++)
            {
                Control c = root.Controls[cx];
                RecursivelyFindWebControls(c, controlToFind, filterID, ref finder);
            }
        }

        public static void RecursivelyFindAllWebControls(Control root, string filterID, ref List<WebControl> finder)
        {
            if (root.GetType().BaseType.Equals(typeof(WebControl)))
            {
                if (root.ID.IndexOf(filterID) > -1)
                    finder.Add((WebControl)root);
                else
                    finder.Add((WebControl)root);
            }

            for (int cx = 0; cx < root.Controls.Count; cx++)
            {
                Control c = root.Controls[cx];
                RecursivelyFindAllWebControls(c, filterID, ref finder);
            }
        }

        public static Control RecursivelyFindTypeOfControl(Control root, Type controlToFind, int currentIndex, int instance)
        {
            if (root.GetType().Equals(controlToFind))
            {
                currentIndex++;
                if (currentIndex == instance)
                    return root;
            }

            foreach (Control c in root.Controls)
            {
                Control t = RecursivelyFindTypeOfControl(c, controlToFind, currentIndex, instance);
                if (t != null)
                    return t;
            }

            return null;
        }

        public static bool DoStringsResembleEachOther(string stringToSplit, string stringToCheck, int threshold)
        {
            char[] allChars = stringToSplit.ToCharArray();
            List<string> groupings = new List<string>();
            int grouper = -1;
            int sIndex = -1;
            foreach (char cx in allChars)
            {
                grouper++;
                if (grouper % threshold == 0)
                {
                    grouper = 0;
                    groupings.Add("");
                    sIndex++;
                }
                groupings[sIndex] += cx.ToString().ToLower();
            }

            foreach (string gr in groupings)
            {
                if (gr.Length == threshold)
                {
                    if (stringToCheck.ToLower().IndexOf(gr) > -1)
                        return true;
                }
            }

            return false;
        }


        public static string StripHTML(string inputString)
        {
            string HTML_TAG_PATTERN = "<.*?>";

            string stripHTML = Regex.Replace
              (inputString, HTML_TAG_PATTERN, string.Empty);

            stripHTML = stripHTML.Replace("&nbsp;", "");

            return stripHTML;
        }

        public static double DateDiff(DateInterval interval, DateTime date1, DateTime date2)
        {
            TimeSpan ts = ts = date2 - date1;
            switch (interval)
            {
                case DateInterval.Year:
                    return date2.Year - date1.Year;
                case DateInterval.Month:
                    return (date2.Month - date1.Month) + (12 * (date2.Year - date1.Year));
                case DateInterval.Weekday:
                    return Fix(ts.TotalDays) / 7;
                case DateInterval.Day:
                    return ts.TotalDays;
                case DateInterval.Hour:
                    return ts.TotalHours;
                case DateInterval.Minute:
                    return ts.TotalMinutes;
                default:
                    return ts.TotalSeconds;
            }
        }

        private static long Fix(double Number)
        {
            if (Number >= 0)
            {
                return (long)Math.Floor(Number);
            }
            return (long)Math.Ceiling(Number);
        }
    }
}